package com.text.demmongodb.service.impl;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.text.demmongodb.entity.User;
import com.text.demmongodb.mapper.UserMapper;
import com.text.demmongodb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Author By wen
 * @Description
 * @Date 2023/10/30 15:36
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private MongoTemplate mongoTemplate;
    @Autowired
    private UserMapper userMapper;

    // 文档集合名称
    private final static String collectionName = "user";

    @Override
    public String insert(User user) {
        //save添加文档
        User save = mongoTemplate.save(user, collectionName);
        //insert 添加文档 - 如果id存在则失败抛出异常
        return save.toString();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateUserInfo(User user) {
        // 设置更新的条件
        Criteria where = Criteria.where("id").is(user.getId());
        Update update = new Update();
        update.set("age", user.getAge());
        //save添加文档 - 如果id存在则更新数据
        UpdateResult result = mongoTemplate.updateFirst(new Query(where),update,User.class, collectionName);
        new RuntimeException();
        return result.toString();
    }

    /**
     * 根据id获取user
     *
     * @param entity
     * @return
     */
    @Override
    public String getById(User entity) {
        User user = mongoTemplate.findById(entity.getId(), User.class, collectionName);
        if(null != user) {
            return user.toString();
        }
        return "无数据！";
    }

    @Override
    public String getListByAge(User user) {
        // 查询age大于某个值的数据
        Criteria ageCriteria = Criteria.where("age").gt(user.getAge());
        // 根据name模糊查询
        Criteria NameCriteria = Criteria.where("name").regex(".*" + user.getName() + ".*");

        // 多条件时，通过andOperator进行添加
        Criteria criterias = new Criteria();
        criterias.andOperator(ageCriteria);
        //criterias.andOperator(NameCriteria);

        Query query = new Query(criterias);

        List<User> userList = mongoTemplate.find(query, User.class, collectionName);
        if(userList != user && userList.size() > 0) {
            return userList.toString();
        }
        return "无数据！";
    }

    @Override
    public String deleteById(User user) {
        Criteria idCriteria = Criteria.where("id").is(user.getId());
        DeleteResult deleteResult = mongoTemplate.remove(new Query(idCriteria), User.class, collectionName);
        return deleteResult.toString();
    }

    @Override
    public String getList() {
        // 分页模型 页码，每页数据条数
        Pageable page = PageRequest.of(0,2, Sort.Direction.DESC, "id");
        Query query = new Query();
        // 得到总条数
        long count = mongoTemplate.count(query, User.class);
        query.with(page);
        // 得到当前页数据list
        List<User> list = mongoTemplate.find(query, User.class, collectionName);
        // 实现分页

        Page<User> pages = PageableExecutionUtils.getPage(list, page, () -> count);
        System.out.println(pages.getSize());
        System.out.println(pages.getContent());

        return list.toString();
    }


}
